//
// Created by martin on 2/10/22.
//

#ifndef MUDUO_EPOLLPOLLER_H
#define MUDUO_EPOLLPOLLER_H

#include "muduo/net/Poller.h"

#include <vector>

struct epoll_event;

namespace muduo
{
namespace net
{

/**
 * IO Multiplexing with epoll(7).
 */
class EPollPoller : public Poller
{
public:
    EPollPoller(EventLoop* loop);
    ~EPollPoller() override;
    Timestamp poll(int timeoutMs, ChannelList* activeChannels) override;
    /* ADD/MOD/DEL */
    void updateChannel(Channel* channel) override;
    /* DEL */
    void removeChannel(Channel* channel) override;

private:
    /* events_数组初始大小 */
    static const int kInitEventListSize = 16;
    /* 将op(EPOLL_CTL_Add/MOD/DEL)转换成字符串 */
    static const char* operationToString(int op);
    /* epoll_wait返回后将就绪的fd添加到激活通道中activeChannels */
    void fillActiveChannels(int numEvents,
                            ChannelList* activeChannels) const;
    /* 由updateChannel/removeChannel调用，真正执行epoll_ctl()控制epoll的函数 */
    void update(int operation, Channel* channel);

    typedef std::vector<struct epoll_event> EventList;
    /* epoll文件描述符，由epoll_create返回 */
    int epollfd_;
    /* epoll事件数组，为了适配epoll_wait参数要求 */
    EventList events_;
};

} // namespace net
} // namespace muduo




#endif //MUDUO_EPOLLPOLLER_H
